/**
 * 客户列表
*/

var player_info = new Object({
    table:null,
    init:function(){
        if(null != this.table)
            return;
        this.queryInfo();
    },
    queryInfo:function(){
        this.table = $('#tb_player_info').bootstrapTable({
            url: contextPath + '/player/info/list/data.json',
            responseHandler: function (res) {
                return res.data;
            },
            queryParams: function (params) {
                return params;
            },
            pagination: true,
            pageList: '[10,20, 50, 100]',
            //showColumns: true,
            showRefresh: true,
            showToggle: true,
            //showPaginationSwitch:true,
            sidePagination: 'server',
            //striped:true,
            sortable: false,
            height: 700,
            search: true,
            searchAlign: 'left',
          /*  toolbar: '<div style="float: left;margin-right:5px;">' +
            '    <button class="btn btn-default" type="button" onclick="agent_mg.add()">' +
            '        <i style="margin-right:2px;" class="fa fa-search"></i>' +
            '        <span>新增</span>' +
            '    </button>' +
            '</div>',*/
            idField: 'id',
            uniqueId: 'id',
            //singleSelect: true,
            clickToSelect: true,
            columns: [{
                field: 'registerTime',
                title: '注册时间',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    return (new Date(value)).Format("yyyy-MM-dd hh:mm")
                }
            }, {
                field: 'nickname',
                title: '昵称',
                width: 100
            }, {
                field: 'openId',
                title: 'OpenId',
                width: 100,
                visible:false
            }, {
                field: 'playCount',
                title: '游戏次数',
                width: 60
            }, {
                field: 'success',
                title: '成功次数',
                width: 60
            }, {
                 field: 'failed',
                 title: '失败次数',
                 width: 60
             }, {
                field: 'inviteCode',
                title: '我的邀请码',
                width: 60
            }, {
                field: 'invitor',
                title: '我的邀请人',
                width: 60
            },{
                field:'status',
                title:'状态',
                width:60,
                formatter:function(value,row,index){
                    var statusHtml = '<input type="checkbox" data-class="switcher-colorGeneratorDemo" ';
                    if(value == 1){
                        statusHtml +='checked="checked"' ;
                    }
                    var id_value = 'tatus_'+row.id;
                    statusHtml +='id="'+id_value+'" name="status" onchange="player_info.changeStatus('+row.id+')"/>';
                    statusHtml += '<script type="text/javascript">' +
                        '$(document).ready(function(){' +
                        '   $("#'+id_value+'").switcher({ theme: \'square\' });' +
                        '});' +
                        '</script>';
                    return statusHtml;
                }
            },{
                field: 'op',
                title: '操作',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    var strHtml = '';
                        //strHtml += '<a href="javascript:void(0);" onclick="client_list.edit('+row.id+',\''+row.name+'\','+row.ratio+')">编辑</a>&nbsp;&nbsp;';
                    strHtml = '<div class="btn-group">' +
                    '<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">更多<i class="fa fa-caret-down"></i></button>' +
                    '   <ul class="dropdown-menu">'+
                    '       <li><a href="javascript:void(0);" onclick="player_info.addressDetails('+row.id+',\''+row.nickname+'\')">收货地址</a></li>'+
                    '       <li><a href="javascript:void(0);" onclick="player_info.invitePlayers('+row.id+',\''+row.nickname+'\')">邀请列表</a></li>'+
                    //'       <li><a href="javascript:void(0);" onclick="">重置财务信息</a></li>'+
                    '   </ul>'+
                    '</div>'
                    return strHtml;
                }
            }]
        });
        $('.search input').attr("placeholder", "微信昵称");
    },
    changeStatus:function(id){
        $.ajax({
            url:contextPath+'/player/info/changeStatus/'+id,
            success:function(res){
                player_info.table.bootstrapTable('refresh');
            },
            error:function(){
                alert_message('修改状态失败，请联系管理员');
            }
        })
    },
    addressDetails:function(playerId,nickname){
        var detailsModal = '<div id="addressDetailsModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog" style="width: 1000px">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="setWxInfoModalLabel">'+nickname+':收货地址列表</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <div class="table-primary">'+
            '                   <table id="tb_address_details"></table>'+
            '               </div>'+
            '           </div>'+
            '</div>'+
            '   </div>'+
            '</div>';
        console.info(detailsModal);

        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(detailsModal)[0]);
        $('#addressDetailsModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#addressDetailsModal')[0]);
        });

        //生成列表
        $("#tb_address_details").bootstrapTable('destroy');
        $("#tb_address_details").bootstrapTable({
            url:contextPath+'/player/address/list_by_play_id/'+playerId,
            columns: [{
                field: 'consignee',
                title: '收件人',
                width: 50,
                align:'center'
            },{
                field:'consigneeTel',
                title:'手机',
                width:100
            },{
                field:'province',
                title:'省',
                width:10
            },{
                field:'city',
                title:'市',
                width:20
            },{
                field:'district',
                title:'区',
                width:20
            },{
                field:'address',
                title:'详细地址',
                width:100
            },{
                field:'isDefault',
                title:'是/否默认',
                width:100,
                formatter:function(value,row,index){
                    if(value == 1){
                        return '是';
                    }else{
                        return '否';
                    }
                }
            }],
            queryParams:function(params){
                return params;
            },
            pagination: true,
            pageList: '[5,10, 15, 20]',
            //showColumns: true,
            showRefresh: true,
            showToggle: true,
            //showPaginationSwitch:true,
            sidePagination: 'server',
            //striped:true,
            sortable: false,
            height: 400,
            //search: true,
            //searchAlign: 'left',
            idField: 'id',
            uniqueId:'id',
            //singleSelect: true,
            clickToSelect: true,
            //toolbar:'<div style="float: left;margin-right:5px;">' +
            //'    <button class="btn btn-default" type="button" onclick="agent_mg.addClient('+playerId+')">' +
            //'        <i style="margin-right:2px;" class="fa fa-search"></i>' +
            //'        <span>新增</span>' +
            //'    </button>' +
            //'</div>',
            responseHandler:function(res){
                return res.data;
            }
        });
        //显示
        $("#addressDetailsModal").modal('show')
    },

    invitePlayers:function(playerId,nickname){
        var invitePlayersModal = '<div id="invitePlayersModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog" style="width: 1000px">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="invitePlayersModalLabel">'+nickname+':邀请列表</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <div class="table-primary">'+
            '                   <table id="tb_invite_players"></table>'+
            '               </div>'+
            '           </div>'+
            '</div>'+
            '   </div>'+
            '</div>';
        console.info(invitePlayersModal);

        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(invitePlayersModal)[0]);
        $('#invitePlayersModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#invitePlayersModal')[0]);
        });

        //生成列表
        $("#tb_invite_players").bootstrapTable('destroy');
        $("#tb_invite_players").bootstrapTable({
            url: contextPath + '/player/info/list_by_invitor/'+playerId,
            responseHandler: function (res) {
                return res.data;
            },
            queryParams: function (params) {
                return params;
            },
            pagination: true,
            pageList: '[10,20, 50, 100]',
            showRefresh: true,
            showToggle: true,
            sidePagination: 'server',
            sortable: false,
            height: 400,
            search: true,
            searchAlign: 'left',
            idField: 'id',
            uniqueId: 'id',
            clickToSelect: true,
            columns: [{
                field: 'registerTime',
                title: '注册时间',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    return (new Date(value)).Format("yyyy-MM-dd hh:mm")
                }
            }, {
                field: 'nickname',
                title: '昵称',
                width: 100
            }, {
                field: 'openId',
                title: 'OpenId',
                width: 100,
                visible:false
            }, {
                field: 'playCount',
                title: '游戏次数',
                width: 60
            }, {
                field: 'success',
                title: '成功次数',
                width: 60
            }, {
                field: 'failed',
                title: '失败次数',
                width: 60
            }, {
                field: 'inviteCode',
                title: '我的邀请码',
                width: 60
            },{
                field:'status',
                title:'状态',
                width:60,
                formatter:function(value,row,index){
                    if(value == 1){
                        return '激活';
                    }
                    return '禁用';
                }
            }]
        });
        //显示
        $("#invitePlayersModal").modal('show')
    }
});